<?php
/*
 * 
 * Notes: There are codes to remove after testing in windows.
 */
class Zendvn_Videotools {
	protected $_inDevWindows	= indevwindows;
	//protected $_inDevWindows	= false;
	
	public function init() {
		
	}
public function convert_video($source, $destination, $scale='320x240', $quality=18, $tinygop=true) {
	       
		chmod($source,0777);
		$gop = '-g 3';
		if(!$tinygop) $gop = '';
		$scaleparam = " -s $scale";
		if($scale == '') $scaleparam = '';
		$command = FFMPEG_PATH . "/ffmpeg -y -i $source -r 30 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -preset fast -crf $quality $gop $scaleparam $destination";

		shell_exec($command);
		

	}
   
public function public_video($source=null, $destination=null, $speed = 1, $scale='320x240',$has_audio = true, $quality = 22, $bitrate = '800k') {
	
if($bitrate == null || !$bitrate) { $bitrate	= '800k'; }
    	      
		
		$tempname = '/tmp/' . md5(time.$source) . '.mp4';
		if($this->_inDevWindows) {
			$tempname = getcwd() . '/tmp/' . md5(time.$source) . '.mp4';
		}
		
		$scale_array = explode('x',$scale);
		
		$audio = '-oac faac';
		
		if(!$has_audio) $audio = '-nosound';
		
    	$command = FFMPEG_PATH . "/mencoder {$source} $audio -ovc x264 -vf unsharp,scale={$scale_array[0]}:{$scale_array[1]} -speed $speed -o {$tempname}";
  	
      if($this->_inDevWindows) {
    		$audio = '-nosound';
    		$command =FFMPEG_PATH . "/mencoder {$source} $audio -ovc x264 -vf unsharp,scale={$scale_array[0]}:{$scale_array[1]} -speed $speed -o {$tempname}";
    	}


    	shell_exec($command);
	
    	$command = FFMPEG_PATH . "/ffmpeg -y -i {$tempname} -acodec libfaac -ab 96k -ac 2 -vcodec libx264 -preset fast -tune film -profile main  -crf {$quality} -b {$bitrate} -r 30 -g 7 {$destination}";
    	
    	if($this->_inDevWindows) {
    		$command = FFMPEG_PATH . "/ffmpeg -y -i {$tempname} -an -vcodec libx264 -vpre fast -crf {$quality} -b {$bitrate} -r 30 -g 7 {$destination}"; //TODO: remove before committing
    	}



    	shell_exec($command);
    	
		shell_exec('rm ' . $tempname);
    }
	public function snap_video($source, $destination, $time) {
		$file= $source;
		ob_start();
		passthru(FFMPEG_PATH . "/ffmpeg -i ".$file." 2>&1");
		$duration = ob_get_contents();
		ob_end_clean();
		$search='/Duration: (.*?)[.]/';
		$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE);
		$duration = $matches[1][0];
		$secs = split('[:]', $duration);
		$secs = (int)$secs[2];
		if($secs <= 5){
			$time = 1;
		}else{
			$time = 5;
		}
		
		$source = escapeshellarg($source);
		$destination = escapeshellarg($destination);
		$command = FFMPEG_PATH . "/ffmpeg -y -i $source -y -sameq -ss $time -t 0.1 -f mjpeg $destination";
		$output = array();
		$return_var = 0;
		$a = exec($command, $output, $return_var);
        return $a;
    }
    
    
    public function resize_snapshot($source, $destination, $dimensions='75x75') {
		$source = escapeshellarg($source);
		$destination = escapeshellarg($destination);
    	$command = "convert $source -resize $dimensions -unsharp 1.5×1.0+1.5+0.02 $destination";
        return shell_exec($command);
    }
    
    public function convert_image ($source, $destination, $dimensions='') {
		$source = escapeshellarg($source);
		$resize = '';
		$destination = escapeshellarg($destination);
		
		if($dimensions != '') $resize = "-resize $dimensions ";
		 
    	$command = "convert $source $resize -unsharp 1.5×1.0+1.5+0.02 $destination";
        return shell_exec($command);
    }
    
    public function crop_snapshot($source, $destination, $crop, $dimensions='75x75') {
		$source = escapeshellarg($source);
		$destination = escapeshellarg($destination);
    	if(!is_array($crop)) return false;
    	
		$command = "convert {$source} -crop {$crop['w']}x{$crop['h']}+{$crop['x']}+{$crop['y']} -resize $dimensions -unsharp 1.5×1.0+1.5+0.02 {$destination} \n";
    	// echo $command;
        return shell_exec($command);
    }
    
    public function fetch_movie_data($file) {
    	$movie = new ffmpeg_movie($file, false);
    	
		$movie_data = array(
			'duration'=> $movie->getDuration()
			, 'frames'=> $movie->getFrameCount()
			, 'framerate'=> $movie->getFrameRate()
			, 'video'=> $movie->hasVideo()
			, 'audio'=> $movie->hasAudio()
		);
		return $movie_data;
    }	
}

/*
 * 
mencoder input.flv -nosound -ovc x264 -vf scale=640:480 -speed 0.5 -o output.mp4
ffmpeg -y -i source.mp4 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre fast -crf 18 -g 30 destination.flv

 */